package com.hzhanghuan.app;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;

import com.hzhanghuan.recyclerview.ItemEventHelper;
import com.hzhanghuan.recyclerview.adapter.ModuleAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/7.
 */
public class ModuleActivity extends AppCompatActivity {


    RecyclerView recyclerView;
    ModuleAdapter.Module<String> stringModule = ModuleAdapter.Module.createModule(String.class, 2);
    ModuleAdapter.Module<Integer> integerModule = ModuleAdapter.Module.createModule(Integer.class, 1);


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        recyclerView = new RecyclerView(this);
        TestModuleAdapter testModuleAdapter = new TestModuleAdapter();
        testModuleAdapter.addModule(stringModule);
        testModuleAdapter.addModule(integerModule);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(testModuleAdapter);
        setContentView(recyclerView);
        ItemEventHelper itemEventHelper = new ItemEventHelper();
        itemEventHelper.setup(recyclerView);
        itemEventHelper.setItemRootViewClickListener(new ItemEventHelper.ItemRootViewClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder) {
                if (viewHolder.getAdapterPosition() == -1) {
                    return;
                }
                if (viewHolder.getItemViewType() == 1) {
                    integerModule.removeItemFromAdapterPosition(viewHolder.getAdapterPosition(), true);
                } else {
                    stringModule.removeItemFromAdapterPosition(viewHolder.getAdapterPosition(), true);
                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("addString");
        menu.add("addInteger");
        menu.add("addStrings");
        menu.add("removeAllString");
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (TextUtils.equals(item.getTitle(), "addString")) {
            stringModule.addItem("String");
            return true;
        } else if (TextUtils.equals(item.getTitle(), "addInteger")) {
            integerModule.addItem(1);
            return true;
        } else if (TextUtils.equals(item.getTitle(), "addStrings")) {
            List<String> strings = new ArrayList<>();
            strings.add("2");
            strings.add("3");
            strings.add("4");
            stringModule.addItems(strings);
            return true;
        } else if (TextUtils.equals(item.getTitle(), "removeAllString")) {
            stringModule.clear(true);
            return true;
        }


        return super.onOptionsItemSelected(item);
    }


}
